package edu.tsinghua.mrpvc.simulate;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.AirConDevice;
import edu.tsinghua.mrpvc.device.CameraDevice;
import edu.tsinghua.mrpvc.device.CurtainDevice;
import edu.tsinghua.mrpvc.device.FrigeDevice;
import edu.tsinghua.mrpvc.device.LightDevice;
import edu.tsinghua.mrpvc.device.MobileDevice;
import edu.tsinghua.mrpvc.device.RFIDDetect;
import edu.tsinghua.mrpvc.device.TvDevice;
import edu.tsinghua.mrpvc.device.simulate.CameraSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.FrigeSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.LightSensorSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.MicSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.MobileSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.PhoneSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.RFIDSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.ServerSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.ThermoSensorSimulateTopic;


public class SimulaterBackHome extends JFrame{
	private ServiceReference ref = null;
	private BundleContext context = null;
	private ServiceTracker tracker;
	public SimulaterBackHome(BundleContext context){
		super("Simulater1");
		this.context = context;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doS1(), BorderLayout.WEST);
		panel.add(doS2(), BorderLayout.CENTER);
		panel.add(doS3toS5(), BorderLayout.EAST);
		panel.add(doS6toS9(), BorderLayout.SOUTH);
		getContentPane().add(panel);
		pack();
		setVisible(true);
	}
	private JPanel doS1(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S1");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S1 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(RFIDSimulateTopic.RFID_DETECT, (Map)null);
					eventAdmin.postEvent(event);
				}				
			}			
		});
		return panel;	
	}
	private JPanel doS3toS5(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doS5(), BorderLayout.EAST);
		panel.add(doS4(), BorderLayout.CENTER);
		//panel.add(doCurtain(), BorderLayout.CENTER);
		panel.add(doS3(), BorderLayout.WEST);
		return panel;
	}
	private JPanel doS6toS9(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doS6(), BorderLayout.WEST);
		panel.add(doS7(), BorderLayout.CENTER);
		panel.add(doS8toS9(), BorderLayout.EAST);
		return panel;
	}
	
	private JPanel doS8toS9(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doS8(), BorderLayout.CENTER);
		panel.add(doS9(), BorderLayout.EAST);
		return panel;
	}
	
	private JPanel doS6(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S6");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S6 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					System.out.println(Calendar.getInstance().getTime().toLocaleString() +
							" Owner: put apple into frige");
					Map<String, String> proper = new Hashtable<String, String>();
					proper.put(FrigeSimulateTopic.PROPER_FOOD_NAME, "Apple");
					Event nextEvent = new Event(FrigeSimulateTopic.FRIGE_FOOD_IN, proper);
					eventAdmin.postEvent(nextEvent);
					
					fwsleep.sleep();
					
					/*
					ref = context.getServiceReference(FrigeDevice.class.getName());
					FrigeDevice frige = (FrigeDevice) context.getService(ref);
					String[] foodInfo = frige.getFoodInfo();
					System.out.print("Food: ");
					for(int i = 0; i < foodInfo.length; i++){
						System.out.print(foodInfo[i] + " ");
					}
					System.out.println("");
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					*/
					System.out.println(Calendar.getInstance().getTime().toLocaleString() +
							" Owner: take apple out of frige");
					proper = new Hashtable<String, String>();
					proper.put(FrigeSimulateTopic.PROPER_FOOD_NAME, "Apple");
					nextEvent = new Event(FrigeSimulateTopic.FRIGE_FOOD_OUT, proper);
					eventAdmin.postEvent(nextEvent);
					
					fwsleep.sleep();				
				}
				
			}			
		});
		return panel;
	}
	private JPanel doS9(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S9");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S9 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
							 " Person: Leave Home");
					Map<String, String> proper = new Hashtable<String, String>();
					Event nextEvent = new Event(RFIDSimulateTopic.RFID_LEAVE, proper);
					eventAdmin.postEvent(nextEvent);
					
					fwsleep.sleep();
				}
				
			}			
		});
		return panel;
	}
	
	private JPanel doS8(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S8");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S8 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> nextProper = new Hashtable<String, String>();
					System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
							" Person: wave hands");
					nextProper.put(CameraSimulateTopic.PROPER_ACTION, CameraSimulateTopic.Constants.ACTION_NEXT);
					Event nextEvent = new Event(CameraSimulateTopic.CAMERA_CTRL_TV, nextProper);
					eventAdmin.postEvent(nextEvent);
					
					fwsleep.sleep();
					
					Map<String, String> biggerProper = new Hashtable<String, String>();
					System.out.println(Calendar.getInstance().getTime().toLocaleString() +
							" Person: wave hands");		
					biggerProper.put(CameraSimulateTopic.PROPER_ACTION, CameraSimulateTopic.Constants.ACTION_BIGGER);
					Event biggerEvent = new Event(CameraSimulateTopic.CAMERA_CTRL_TV, biggerProper);
					eventAdmin.postEvent(biggerEvent);
				}
				
			}			
		});
		return panel;
	}
	private JPanel doS7(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S7");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S7 started");
				
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> proper = new Hashtable<String, String>();
					proper.put(MobileSimulateTopic.PROPER_PATH, "c:/photos/");
					Event event = new Event(MobileSimulateTopic.MOBILE_SHARE_PATH, proper);
					eventAdmin.postEvent(event);
				}
				
				fwsleep.sleep();
			}			
		});
		return panel;
	}
	
	private JPanel doS5(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S5");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S5 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(RFIDSimulateTopic.RFID_VISIT, (Map)null);
					eventAdmin.postEvent(event);
				}
				
				fwsleep.sleep();
				/*
				ref = context.getServiceReference(RFIDDetect.class.getName());
				RFIDDetect rfid = (RFIDDetect) context.getService(ref);
				rfid.openDoor();
				*/
			}			
		});
		return panel;
	}
	
	private JPanel doS3(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S3");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S3 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(ServerSimulateTopic.SERVER_PHONE_BEFORE_TV, (Map)null);
					eventAdmin.postEvent(event);
	
					
					//event = new Event(SimulateTopic.MOBILE_CTRL_TV, (Map)null);
					//eventAdmin.postEvent(event);
				}
				
				fwsleep.sleep();
				
				/*
				ref = context.getServiceReference(TvDevice.class.getName());
				TvDevice tv = (TvDevice) context.getService(ref);
				System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
				" Mobilephone: control TV powerOn");
				tv.powerOn();
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
				" Mobilephone: control TV upChannel");
				tv.upChannel();
			
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				*/
				if(eventAdmin != null){
					Event event = new Event(ServerSimulateTopic.SERVER_WATCH_TV, (Map)null);
					eventAdmin.postEvent(event);
				}
				
				fwsleep.sleep();
			}			
		});
		return panel;
	}
	
	private JPanel doS4(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S4");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S4 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(PhoneSimulateTopic.PHONE_RING, (Map)null);
					eventAdmin.postEvent(event);
				}
				//Thread.sleep(1000);
				
			}			
		});
		return panel;
		
	}
	private JPanel doS2(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("S2");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("S2 started");
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					System.out.println(Calendar.getInstance().getTime().toLocaleString() +
							" Owner pointed to curtain and says \"OPEN\"");
					Map<String, String> proper = new Hashtable<String, String>();
					proper.put(CameraSimulateTopic.PROPER_DIRECTION,  CameraSimulateTopic.Constants.DIR_WEST);
					Event event = new Event(CameraSimulateTopic.CAMERA_CTRL_CURTAIN, proper);
					eventAdmin.postEvent(event);
					
					fwsleep.sleep();
					
					event = new Event(MicSimulateTopic.MIC_OPEN, (Map)null);
					eventAdmin.postEvent(event);
					
					fwsleep.sleep();
				}
				//Thread.sleep(1000);
				
			}			
		});
		return panel;			
	}
	
	private JPanel doRFID(){
		JPanel panel = new JPanel();
		JButton btnDetect = new JButton("RFID:Detect");
		panel.add(btnDetect);
		btnDetect.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(RFIDSimulateTopic.RFID_DETECT, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doThermoSensor(){
		JPanel panel = new JPanel();
		JButton btnSense = new JButton("ThermoSensor:Sense");
		panel.add(btnSense);
		btnSense.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(ThermoSensorSimulateTopic.THERMO_SENSE, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doLightSensorAndLightAndCurtainAndAirCon(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doAirCon(), BorderLayout.WEST);
		panel.add(doLightSensor(), BorderLayout.CENTER);
		panel.add(doLightAndCurtain(), BorderLayout.EAST);		
		return panel;
	}
	
	private JPanel doLightAndCurtain(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doLight(), BorderLayout.WEST);
		panel.add(doCurtain(), BorderLayout.CENTER);
		return panel;
	}
	
	private JPanel doCurtain(){
		JPanel panel = new JPanel();
		JButton btnOpen = new JButton("Curtain");
		panel.add(btnOpen);
		btnOpen.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(CurtainDevice.class.getName());
				if(ref != null){
					CurtainDevice curtain = (CurtainDevice) context.getService(ref);
					curtain.open();
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doAirCon(){
		JPanel panel = new JPanel();
		JButton btnUpTemperature = new JButton("AirCondition");
		panel.add(btnUpTemperature);
		btnUpTemperature.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.powerOn();
					
					fwsleep.sleep();
					
					aircon.tuneMode();
					
					fwsleep.sleep();
					
					aircon.tuneWind();
					
					fwsleep.sleep();
					
					aircon.upTemperature();
					
					fwsleep.sleep();
				}
			}			
		});
		return panel;
	}
	
	private JPanel doLightSensor(){
		JPanel panel = new JPanel();
		JButton btnSense = new JButton("LightSensor");
		panel.add(btnSense);
		btnSense.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(LightSensorSimulateTopic.LIGHT_SENSE, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doLight(){
		JPanel panel = new JPanel();
		JButton btnUpLevel = new JButton("Light");
		panel.add(btnUpLevel);
		btnUpLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.powerOn();
					
					fwsleep.sleep();
					
					light.upLevel();
				}
			}			
		});
		return panel;
	}
	
	private static class fwsleep{
		public static int interval = 1000;
		public static boolean work = false;
		public static void sleep(){
			if(work){
				try {
					Thread.sleep(interval);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			
		}
	}
	
}


